<template>
  <div class="register-container">
    <!-- 注册内容 -->
    <div class="register">
      <h3>
        注册新用户
        <span class="go"
          >我有账号，去 <router-link to="login">登陆</router-link>
        </span>
      </h3>
      <form autocomplete="off" v-on:submit.prevent>
      <div class="content">
        <label>用户名</label>
        <input
          type="text"
          placeholder="请输入用户名"
          v-model="buyerName"
          name="buyerName"
          required
          v-validate="{ required: true }"
          :class="{ invalid: errors.has('buyerName') }"
        /><a> 必填</a>
        <span class="error-msg">{{ errors.first("buyerName") }}</span>
      </div>
      <div class="content">
        <label>账号</label>
        <input
          type="text"
          placeholder="请输入账号(用于登录)"
          v-model="buyerId"
          name="buyerId"
          required
          v-validate="{
            required: true,
            regex: /^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]{8,20}$/,
          }"
          :class="{ invalid: errors.has('buyerId') }"
        /><a> 8到20位字母加数字</a>
        <span class="error-msg">{{ errors.first("buyerId") }}</span>
      </div>
      <div class="content">
        <label>登录密码:</label>
        <input
          type="password"
          placeholder="请输入你的登录密码"
          v-model="buyerPw"
          name="buyerPw"
          required
          v-validate="{
            required: true,
            regex: /^(?=.*[a-zA-Z])(?=.*[0-9])[A-Za-z0-9]{8,20}$/,
          }"
          :class="{ invalid: errors.has('buyerPw') }"
        /><a> 8到20位字母加数字</a>
        <span class="error-msg">{{ errors.first("buyerPw") }}</span>
      </div>
      <div class="content">
        <label>确认密码:</label>
        <input
          type="password"
          placeholder="请输入确认密码"
          v-model="rebuyerPw"
          name="rebuyerPw"
          required
          v-validate="{ required: true, is: buyerPw }"
          :class="{ invalid: errors.has('rebuyerPw') }"
        />
        <span class="error-msg">{{ errors.first("rebuyerPw") }}</span>
      </div>
      <div class="content">
        <label>手机号:</label>
        <input
          type="text"
          placeholder="请输入你的手机号"
          v-model="buyerPhone"
          name="buyerPhone"
          required
          v-validate="{ required: true, regex: /^1\d{10}$/ }"
          :class="{ invalid: errors.has('buyerPhone') }"
        /><a> 必填</a>
        <span class="error-msg">{{ errors.first("buyerPhone") }}</span>
      </div>
      <div class="content">
        <label>地址:</label>
        <input
          type="text"
          placeholder="请输入你的地址"
          v-model="buyerAddress"
          name="buyerAddress"
          required
          v-validate="{ required: true }"
          :class="{ invalid: errors.has('buyerAddress') }"
        /><a> 必填</a>
        <span class="error-msg">{{ errors.first("buyerAddress") }}</span>
      </div>
      <div class="btn">
        <button @click="buyerRegister">完成注册</button>
      </div>
    </form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Register",
  data() {
    return {
      buyerName: "", //用户名
      buyerId: "", //账号
      buyerPw: "", //密码
      rebuyerPw: "", //第二次输入密码
      buyerAddress: "", //地址
      buyerPhone: "", //手机
    };
  },
  methods: {
    //用户注册
    async buyerRegister() {
      //所有表单验证成功再发请求
      const success = await this.$validator.validateAll();
      if (success) {
        try {
          //如果成功路由跳转到登陆
          const { buyerName, buyerId, buyerPw, buyerAddress, buyerPhone } =
            this;

          await this.$store.dispatch("buyerRegister", {
            buyerName,
            buyerId,
            buyerPw,
            buyerAddress,
            buyerPhone,
          });
          this.$router.push("/login");
        } catch (error) {
          alert(error.message);
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.register-container {
  .register {
    width: 1200px;
    height: 550px;
    border: 1px solid rgb(223, 223, 223);
    margin: 0 auto;

    h3 {
      background: #ececec;
      margin: 0;
      padding: 6px 15px;
      color: #333;
      border-bottom: 1px solid #dfdfdf;
      font-size: 20.04px;
      line-height: 30.06px;

      span {
        font-size: 14px;
        float: right;

        a {
          color: #e1251b;
        }
      }
    }

    div:nth-of-type(1) {
      margin-top: 40px;
    }

    .content {
      padding-left: 390px;
      margin-bottom: 18px;
      position: relative;

      label {
        font-size: 14px;
        width: 96px;
        text-align: right;
        display: inline-block;
      }

      input {
        width: 270px;
        height: 38px;
        padding-left: 8px;
        box-sizing: border-box;
        margin-left: 5px;
        outline: none;
        border: 1px solid #999;
      }

      img {
        vertical-align: sub;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .controls {
      text-align: center;
      position: relative;

      input {
        vertical-align: middle;
      }

      .error-msg {
        position: absolute;
        top: 100%;
        left: 495px;
        color: red;
      }
    }

    .btn {
      text-align: center;
      line-height: 36px;
      margin: 17px 0 0 55px;

      button {
        outline: none;
        width: 270px;
        height: 36px;
        background: #e1251b;
        color: #fff !important;
        display: inline-block;
        font-size: 16px;
      }
    }
  }

  .copyright {
    width: 1200px;
    margin: 0 auto;
    text-align: center;
    line-height: 24px;

    ul {
      li {
        display: inline-block;
        border-right: 1px solid #e4e4e4;
        padding: 0 20px;
        margin: 15px 0;
      }
    }
  }
}
</style>